package com.gpshistory;

import android.app.Service;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.util.Log;

public class GPSReader extends Service {
	private Thread task;
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		finish();
	}
	
	private void finish(){
		if(task != null){
			try{
				task.interrupt();
			}catch (Exception ignored) {}
		}
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		finish();
		task = new Thread(new Runnable() {
			public void run() {
				try{
					ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

					while(!Thread.currentThread().isInterrupted()){
						NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
						Log.i("GPSReader", "WI-FI available = "+mWifi.isAvailable());
						Thread.sleep(5000);
						
					}
				}catch (Exception e) {
					Log.i("GPSReader", "Terminado", e);
				}
			}
		});
		
		task.start();
		
		return START_STICKY;
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
